package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.bo.MonthlyAttendanceStatBo;
import org.eiahe.hr.attendance.domain.vo.MonthlyAttendanceStatVo;
import org.eiahe.hr.attendance.service.IMonthlyAttendanceStatService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 月度考勤统计Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/monthlyAttendanceStat")
public class MonthlyAttendanceStatController extends BaseController {

    private final IMonthlyAttendanceStatService monthlyAttendanceStatService;

    /**
     * 查询月度考勤统计列表
     */
    @SaCheckPermission("attendance:monthlyStat:list")
    @GetMapping("/list")
    public TableDataInfo<MonthlyAttendanceStatVo> list(MonthlyAttendanceStatBo bo, PageQuery pageQuery) {
        return monthlyAttendanceStatService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出月度考勤统计列表
     */
    @SaCheckPermission("attendance:monthlyStat:export")
    @Log(title = "月度考勤统计", linkType = "attendance", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MonthlyAttendanceStatBo bo, HttpServletResponse response) {
        List<MonthlyAttendanceStatVo> list = monthlyAttendanceStatService.queryList(bo);
        ExcelUtil.exportExcel(list, "月度考勤统计", MonthlyAttendanceStatVo.class, response);
    }

    /**
     * 获取月度考勤统计详细信息
     */
    @SaCheckPermission("attendance:monthlyStat:query")
    @GetMapping("/{statId}")
    public R<MonthlyAttendanceStatVo> getInfo(@PathVariable Long statId) {
        return R.ok(monthlyAttendanceStatService.queryById(statId));
    }

    /**
     * 重新生成指定员工的月度统计
     */
    @SaCheckPermission("attendance:monthlyStat:regenerate")
    @Log(title = "重新生成月度考勤统计", linkType = "attendance", businessType = BusinessType.UPDATE)
    @PostMapping("/regenerate")
    public R<Void> regenerate(@RequestParam String employeeNo,
            @RequestParam Integer year,
            @RequestParam Integer month) {
        monthlyAttendanceStatService.regenerateEmployeeStats(employeeNo, year, month);
        return R.ok();
    }

    /**
     * 生成指定月份的所有员工统计
     */
    @SaCheckPermission("attendance:monthlyStat:generate")
    @Log(title = "生成月度考勤统计", linkType = "attendance", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    public R<Void> generate(@RequestParam Integer year, @RequestParam Integer month) {
        monthlyAttendanceStatService.generateMonthlyStats(year, month);
        return R.ok();
    }
}
